package org.webscale.amazinggwt.client.login;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;

public class LoginManager {
	public static LoginManager self = new LoginManager();
	
	private List<LoginListener> listeners = null;
	
	public static LoginManager instance(){
		return self;
	}
	
	
	public static SiteUser getSiteUser(){
		return Registry.get(SiteUser.REGISTRY_ID);
	}
	
	/**
	 * Controller Method for SiteUser. Fires off event
	 * @param user
	 */
	public static void login(SiteUser user){
		Registry.register(SiteUser.REGISTRY_ID, user);
		
		if(self.listeners == null){
			return;
		}
		for(LoginListener l : self.listeners){
			l.onUserUpdate(user);
		}
	}
	
	public void addLoginListener(LoginListener l){
		if(listeners == null){
			listeners = new ArrayList<LoginListener>();
		}
		listeners.add(l);
	}
	
	
	
}
